package cn.itcast.aspect;

import cn.itcast.pojo.MyProceedingJoinPoint;
import cn.itcast.pojo.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component
public class MyAdvice {

    public Object around(MyProceedingJoinPoint pjp) {
        try {
            //1.篡改参数
            Object[] args = pjp.getArgs();
            args[0] = (Integer) args[0] + 100;
            System.out.println("前置增强");
            //2.调用目标方法
            Object result = pjp.proceed(args);
            //3.篡改返回值
            Result rs = (Result) result;
            rs.setMsg(((Result) result).getMsg() + "我篡改了你的返回值哦");
            System.out.println("返回增强");
            return rs;
        } catch (Throwable e) {
            System.out.println("异常增强");
            throw new RuntimeException(e);
        } finally {
            System.out.println("后置增强");
        }
    }
}
